#!/bin/env bash
#rofi -dmenu -width 40 -lines 0 -mesg "$(getoutput)" -p "sus: suspend | lock: lock screen"


PROFILE_USR=dm
TZ=Europe/Saratov
REPOMIRROR=Russia

#set -euxo pipefail


SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

if [[ $EUID -ne 0 ]]; then
   echo "This script must be run as root. Syntax: sudo setup.sh dmmini|dmhome|dmnote"; exit 1
fi

if [[ -z $1 ]]; then
    echo -e "No parameter passed.\nChoose profile for setting up. Syntax: setup.sh dmmini|dmhome|dmnote"; exit 1;
else
  if [ "$1" == "dmmini" ] || [ "$1" == "dmhome" ] || [ "$1" == "dmnote" ]
    then
      PROFILE=$1; echo "Profile is $PROFILE"
    else
      echo -e "No correct parameter passed.\nChoose profile for setting up. Syntax: sudo setup.sh dmmini|dmhome|dmnote"; exit 1
  fi
fi

set -x

sudoer_setup()
{
  rm -f /etc/sudoers.d/wheel 2> /dev/null
  echo '%wheel ALL=(ALL:ALL) NOPASSWD: ALL' > /etc/sudoers.d/wheel
  chmod 440 /etc/sudoers.d/wheel
  usermod -aG wheel ${PROFILE_USR}
}

tz_setup()
{
  timedatectl set-timezone ${TZ}
  hwclock --systohc
  timedatectl set-ntp true
}

hostname_setup()
{
  echo $PROFILE > /etc/hostname
  echo -e "127.0.0.1 localhost\n::1 localhost\n127.0.0.1 ${PROFILE}.localdomain ${PROFILE}\n" > /etc/hosts
}

locale_setup()
{
  if [ "$PROFILE" == "dmmini" ]; then
    consolefont=ter-k32b
  else
    consolefont=ter-k16
  fi
  echo -e "en_US.UTF-8 UTF-8\nru_RU.UTF-8 UTF-8" > /etc/locale.gen
  locale-gen
  echo "LANG=ru_RU.UTF-8" > /etc/locale.conf
  echo -e "KEYMAP=ru\nFONT=${consolefont}" > /etc/vconsole.conf
}

pacman_setup()
{
  cat ${SCRIPT_DIR}/etc/pacman.conf > /etc/pacman.conf

  pacman-key --recv-key FBA220DFC880C036 --keyserver keyserver.ubuntu.com
  pacman-key --lsign-key FBA220DFC880C036
  pacman -U --noconfirm 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-keyring.pkg.tar.zst' 'https://cdn-mirror.chaotic.cx/chaotic-aur/chaotic-mirrorlist.pkg.tar.zst'
  cat ${SCRIPT_DIR}/etc/pacman.d/chaotic-mirrorlist > /etc/pacman.d/chaotic-mirrorlist

  pacman -Syy
  pacman -S --noconfirm --needed reflector
  reflector --country ${REPOMIRROR} -l 5 -p http --sort rate --save /etc/pacman.d/mirrorlist
  echo -e "--save /etc/pacman.d/mirrorlist\n--country ${REPOMIRROR}\n--protocol https\n--latest 5\n" > /etc/xdg/reflector/reflector.conf
  systemctl enable --now reflector.timer
  systemctl enable --now reflector

  pacman -S --noconfirm --needed paru

}

packages_setup()
{
  if [ "$PROFILE" == "dmmini" ]; then
    PKGS="base base-devel linux-zen linux-zen-headers linux-firmware intel-ucode iucode-tool openssh terminus-font dosfstools \
    man-db man-pages python-pip bash-completion pipewire pipewire-jack pipewire-pulse networkmanager networkmanager-openvpn \
    ttf-hack ntfs-3g ncdu exa tldr bat htop atop iftop powertop sshpass neovim unzip unrar p7zip \
    mesa xorg xorg-xinit openbox rofi kitty pcmanfm falkon x11vnc" #archlinux-xdg-menu
  else # заменить список для профилей
    PKGS="base base-devel linux-zen linux-zen-headers linux-firmware intel-ucode iucode-tool openssh terminus-font dosfstools \
    man-db man-pages python-pip bash-completion pipewire pipewire-jack pipewire-pulse networkmanager networkmanager-openvpn \
    ttf-hack ntfs-3g ncdu exa tldr bat htop atop iftop powertop sshpass neovim unzip unrar p7zip \
    mesa xorg xorg-xinit openbox rofi kitty pcmanfm falkon x11vnc" #archlinux-xdg-menu
  fi
  pacman -Syy --noconfirm --needed $PKGS
}

login_setup()
{
  rm -rf /etc/systemd/system/getty@tty1.service.d
  mkdir /etc/systemd/system/getty@tty1.service.d
  echo -e "[Service]\nExecStart=\nExecStart=-/sbin/agetty -o '-p -f -- \\u' --noclear --autologin ${PROFILE_USR} - \$TERM\n" > /etc/systemd/system/getty@tty1.service.d/autologin.conf
  XINITRC=/home/${PROFILE_USR}/.xinitrc
  echo -e "exec openbox-session" > ${XINITRC} && chmod +x ${XINITRC} && chown ${PROFILE_USR}:${PROFILE_USR} ${XINITRC}
}

remote_control_setup()
{
  systemctl enable --now sshd
}

openbox_setup()
{
  rm -rf /usr/share/themes/*
  cp -r ${SCRIPT_DIR}/usr/share/themes/* /usr/share/themes/
  #su - ${PROFILE_USR} -c "paru -S --noconfirm --needed openbox-arc-git"
  rm -rf /home/${PROFILE_USR}/.config/openbox
  cat ${SCRIPT_DIR}/etc/xdg/openbox/rc.xml > /etc/xdg/openbox/rc.xml
  cat ${SCRIPT_DIR}/etc/xdg/openbox/menu.xml > /etc/xdg/openbox/menu.xml
  cat ${SCRIPT_DIR}/etc/xdg/openbox/autostart > /etc/xdg/openbox/autostart
  cat ${SCRIPT_DIR}/dotfiles/.bash_profile > /home/${PROFILE_USR}/.bash_profile
  cat ${SCRIPT_DIR}/dotfiles/.bashrc > /home/${PROFILE_USR}/.bashrc
}


#Для использования шрифта в пользовательском пространстве при загрузке системы используйте хук consolefont в /etc/mkinitcpio.conf. Смотрите mkinitcpio (Русский)#HOOKS для подробностей.


sudoer_setup
tz_setup
hostname_setup
locale_setup
pacman_setup
packages_setup
login_setup
remote_control_setup
openbox_setup

systemctl reboot
